const api = require('../../../../ws_micro_shop/utils/api.js');
const app = getApp();

Page({
  data: {
    status: 1,
    day: 1,
    current: '',
    start: '',
    end: '',
  },
  onLoad() {
    let that = this;
    app.loadCurrencyUnit().then(res => {
      that.setData({
        unit: res.unit,
      });
    });
    that.calcDate(that.data.day);
    that.onLoadData(that.data.day);
  },
  switchTab(e) {
    let { status, day } = e.currentTarget.dataset;
    this.setData({
      day,
      status,
    });
    this.calcDate(day);
    this.onLoadData(day);
  },
  onLoadData(day) {
    let that = this;
    app.ws_micro_shoprequest({
      url: api.adminer.analysis,
      data: {
        day,
      },
      success: function (res) {
        if (res.code === 0) {
          that.setData(res.data);
        }
      },
    });
  },
  calcDate(day) {
    let current = new Date().toLocaleDateString();
    let end = new Date(current).getTime() - 1000;
    let start = new Date(current).getTime() - day * 24 * 60 * 60 * 1000;
    this.setData({
      current,
      start: new Date(start).toLocaleDateString(),
      end: new Date(end).toLocaleDateString(),
    });
  },
});
